package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.FansAndLike;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: MyFriendingListAdapter
 * @Description: @我的好友数据列表
 * @Author: ljp
 * @CreateDate: 2018/10/18 14:33
 * @Version: 1.0
 */
public class MyFriendingListAdapter extends BaseAdapter<FansAndLike.PageBean.ListBean> {
    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public MyFriendingListAdapter(Context context, List<FansAndLike.PageBean.ListBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            FansAndLike.PageBean.ListBean listBean = tlist.get(position);
            if (!TextUtils.isEmpty(listBean.getAvatarUrl())) {
                if (!listBean.getAvatarUrl().equals(itemViewHolder.imageIcon.getTag(R.id.image_icon))) {
                    // 加载图片
                    itemViewHolder.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getAvatarUrl(),
                            itemViewHolder.imageIcon);
                    //setImage(listBean.getAvatarUrl(), viewHolder1.imageIcon);
                }
            }
            if(!TextUtils.isEmpty(listBean.getGender())) {
                if (listBean.getGender().equals("1")) {//判断性别  1男 2女
                    itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                    itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
                } else if (listBean.getGender().equals("2")) {
                    itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                    itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
                }
            }
            if (!TextUtils.isEmpty(listBean.getUType()) && listBean.getUType().equals("2")) {//判断是否是主播
                itemViewHolder.imageIsAnchor.setVisibility(View.VISIBLE);
            } else {
                itemViewHolder.imageIsAnchor.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(tlist.get(position).getBirthday())) {
                itemViewHolder.tvAge.setText(Utils.getAge(tlist.get(position).getBirthday()));
            }
            if(!TextUtils.isEmpty(listBean.getVType())){
                if (listBean.getVType().equals("1")) {//判断是vip等级 1 月 2 年 0 不是会员
                    itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                    itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
                } else if (listBean.getVType().equals("2")) {
                    itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                    itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
                } else {
                    itemViewHolder.imageVip.setVisibility(View.GONE);
                }
            }
            itemViewHolder.tvNikeName.setText(listBean.getNickName());

            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClick.setItemClick(v,listBean);
                }
            });

        }

    }

    class ItemViewHolder extends BaseViewHolder{
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        View itemView;
        @BindView(R.id.image_is_anchor)
        ImageView imageIsAnchor;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            this.itemView = itemView;

        }
    }
    public interface ItemClick{
        void setItemClick(View v,FansAndLike.PageBean.ListBean listBean);
    }
}
